/*
RBISQuery
Small AJAX library for RBis app
+JQuery required
+Author: Nor� Moreno
*/
/////// Text Field BLA
//Clickable Object
function clickable()
{
  this.clicked = false;
  this.original = "";
}

//Remove default text in a text input element
function removeDefaultText(textfield, clickableObj)
{
  if(clickableObj.clicked==false){
    clickableObj.original=textfield.value;
    textfield.value="";
    clickableObj.clicked=true;
  }
}
//Recover default text in a text input element
function recoverDefaultText(textfield, clickableObj)
{
  if(!textfield.value){
    clickableObj.clicked=false;
    textfield.value=clickableObj.original;
  }
}


//// Loader for element, with ajax responsa


//Clickable Object
function XHRHandler()
{
  var xhr_object;
}

function XMLContainer()
{
  var xmlString;
}


/*
 * Make a post and include success data inside the html object with the id give in element_content
 *
 * Show loader in element_loader while everything is processed
 * nestedFunction, is some function to run after the update of the text in element_content
 */
function loadAndShow(xhr_handler, data, action, element_content, element_loader, nestedFunction)
{
  $('#'+element_loader).fadeIn();
  //$('#'+element_content).hide();

   //@todo nested abort
   //prevent having same request multiple times
  if(xhr_handler.xhr_object) {
    xhr_handler.xhr_object.abort();
  }
  
  xhr_handler.xhr_object = $.post(
    action,
    data,
    function(response){
      $('#'+element_loader).hide();
      $('#'+element_content).html(response);
      if(!(nestedFunction  === undefined)){
        nestedFunction();
      }
      $('#'+element_content).fadeIn();
    }
    );
}

// Add unique function to an array, to return unique
Array.prototype.unique = function(){
    var vals = this;
    var uniques = [];
    for(var i=vals.length;i--;){
        var val = vals[i];
        if($.inArray( val, uniques )===-1){
            uniques.unshift(val);
        }
    }
    return uniques;
}

Array.prototype.find = function(searchStr) {
  var returnArray = false;
  for (i=0; i<this.length; i++) {
    if (typeof(searchStr) == 'function') {
      if (searchStr.test(this[i])) {
        if (!returnArray) { returnArray = [] }
        returnArray.push(i);
      }
    } else {
      if (this[i]===searchStr) {
        if (!returnArray) { returnArray = [] }
        returnArray.push(i);
      }
    }
  }
  return returnArray;
}